package org.loofer.ext.utils;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public final class ToastUtils {
    private static final int DEFAULT_GRAVITY = 81;
    public static final int LENGTH_LONG = 1;
    public static final int LENGTH_SHORT = 0;
    private static final Singleton<Toast, Context> sToast = new Singleton<Toast, Context>() {
        protected Toast create(Context context) {
            Toast toast = null;
            if (!(context == null || context.getApplicationContext() == null)) {
                try {
                    toast = Toast.makeText(context.getApplicationContext(), null, 0);
                } catch (Throwable th) {
                }
            }
            return toast;
        }
    };

    private ToastUtils() {
    }

    public static Toast get(Context context) {
        return (Toast) sToast.get(context);
    }

    public static void show(Context context, int resId) {
        show(context, resId, 0);
    }

    public static void show(Context context, CharSequence msg) {
        show(context, msg, 0);
    }

    public static void show(Context context, int resId, int duration) {
        show(context, resId, duration, 81);
    }

    public static void show(Context context, CharSequence msg, int duration) {
        show(context, msg, duration, 81);
    }

    public static void show(Context context, int resId, int duration, int gravity) {
        show(context, resId == 0 ? null : getString(context, resId), duration, gravity);
    }

    public static void show(Context context, final CharSequence msg, final int duration, final int gravity) {
        if (msg != null && msg.length() != 0 && shouldShow(context)) {
            final Context appContext = context.getApplicationContext();
            if (ThreadUtils.isMainThread()) {
                showImmediately(appContext, msg, duration, gravity);
            } else {
                ThreadUtils.runOnUiThread(new Runnable() {
                    public void run() {
                        ToastUtils.showImmediately(appContext, msg, duration, gravity);
                    }
                });
            }
        }
    }

    public static void show(Activity activity, int resId) {
        show(activity, resId, 0);
    }

    public static void show(@Nullable Activity activity, CharSequence msg) {
        show(activity, msg, 0);
    }

    public static void show(@Nullable Activity activity, int resId, int duration) {
        show(activity, resId == 0 ? null : getString(activity, resId), duration, 81);
    }

    public static void show(@Nullable Activity activity, CharSequence msg, int duration) {
        show(activity, msg, duration, 81);
    }

    public static void show(@Nullable Activity activity, int resId, int duration, int gravity) {
        show(activity, resId == 0 ? null : getString(activity, resId), duration, gravity);
    }

    public static void show(@Nullable Activity activity, final CharSequence msg, final int duration, final int gravity) {
        if (msg != null && msg.length() != 0 && shouldShow(activity)) {
            final Context appContext = activity.getApplicationContext();
            if (ThreadUtils.isMainThread()) {
                showImmediately(appContext, msg, duration, gravity);
            } else {
                ThreadUtils.runOnUiThread(new Runnable() {
                    public void run() {
                        ToastUtils.showImmediately(appContext, msg, duration, gravity);
                    }
                });
            }
        }
    }

    private static void showImmediately(Context context, CharSequence msg, int duration, int gravity) {
        Toast toast = (Toast) sToast.get(context);
        if (toast != null) {
            try {
                toast.setText(msg);
                toast.setDuration(duration);
                toast.setGravity(gravity, toast.getXOffset(), toast.getYOffset());
                toast.show();
            } catch (Throwable th) {
            }
        }
    }

    private static boolean shouldShow(Context context) {
        if (context != null && context == context.getApplicationContext() && (context instanceof Activity)) {
            return shouldShow((Activity) context);
        }
        return true;
    }

    private static boolean shouldShow(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return false;
        }
        Window window = activity.getWindow();
        if (window == null) {
            return false;
        }
        View decorView = window.getDecorView();
        if (decorView == null || decorView.getVisibility() != 0) {
            return false;
        }
        return true;
    }

    private static String getString(Context context, int resId) {
        return context.getString(resId);
    }
}
